package com.demo.lab.controller;


import com.demo.lab.entity.base.Result;
import com.demo.lab.entity.base.ResultTable;
import com.demo.lab.entity.base.ResultTree;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
@RestController
public class BaseController {

    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    @ExceptionHandler(value = Exception.class)
    public Result<Void> globalExceptionHandle(Exception e) {
        logger.error("发生异常:", e);
        return Result.failure("非常抱歉,服务器异常,请联系客服人员。");
    }


    /**
     * 页面跳转
     */
    public ModelAndView jumpPage(String path) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(path);
        return modelAndView;
    }

    /**
     * Describe: 返回数据表格数据 分页
     * Param data
     * Return 表格分页数据
     */
    protected static ResultTable pageTable(Object data, long count) {
        return ResultTable.pageTable(count, data);
    }

    /**
     * Describe: 返回 Tree 数据
     * Param data
     * Return Tree数据
     */
    protected static ResultTree dataTree(Object data) {
        ResultTree resuTree = new ResultTree();
        resuTree.setData(data);
        return resuTree;
    }

    /**
     * Describe: 返回数据表格数据
     * Param data
     * Return 表格分页数据
     */
    protected static ResultTable dataTable(Object data) {
        return ResultTable.dataTable(data);
    }

    /**
     * 选择返回
     */
    public Result decide(Boolean b) {
        return Result.decide(b);
    }

    /**
     * 失败操作
     */
    public Result failure(String msg) {
        return Result.failure(msg);
    }

}
